import { isEmptyVal } from './is'
export function generatePassword(length = 10) {
  const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
  const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz'.split('')
  const symbolChars = '!@#$%^&*()_+-=[]{}|;:,.<>?'.split('')
  let password = ''

  password += uppercaseChars[Math.floor(Math.random() * uppercaseChars.length)]
  password += lowercaseChars[Math.floor(Math.random() * lowercaseChars.length)]
  password += symbolChars[Math.floor(Math.random() * symbolChars.length)]

  const allChars = uppercaseChars.concat(lowercaseChars, symbolChars)
  for (let i = password.length; i < length; i++) {
    password += allChars[Math.floor(Math.random() * allChars.length)]
  }

  const shuffledPassword = password
    .split('')
    .sort(() => Math.random() - 0.5)
    .join('')

  return shuffledPassword
}

// 数据脱敏存储
export function maskString(input: string): string {
  if (isEmptyVal(input)) {
    return ''
  }
  const length = input.length
  if (length < 2) {
    return input
  } else if (length === 2) {
    return input[0] + '*'
  } else if (length === 3 || length === 4) {
    return `${input[0]}*${input[length - 1]}`
  } else {
    const starsCount = length - 6
    return `${input.substring(0, 3)}${'*'.repeat(starsCount)}${input.substring(length - 3)}`
  }
}
